<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>js冒泡排序</title>
	</head>

	<body>
		<script>
			var array = [5, 3, 4, 2, 1];
			var temp = 0;
			for(var i = 0; i < array.length; i++) { //设置总循环数
				for(var j = 0; j < array.length - i; j++) { //设置每次循环需要排序几次
					if(array[j] > array[j + 1]) { //比较第j个和j+1个数，当j>j+1时进行换位
						temp = array[j + 1];
						array[j + 1] = array[j]; //换位过程
						array[j] = temp;
					}
				}
			}

			for(var i = 0; i < array.length; i++) {
				console.log(array[i] + " "); //按现在的顺序输出数列
			}
		</script>
	</body>

</html>